/**
 * 
 */
package gui.text;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

/**
 * Subclass of RootPane deigned to be stored within either a RootPane or something that inherits from one.
 * 
 * This class adds two pieces of functionality to RootPane
 * 1. Upon reaching the end of the text within one of these Panes the arrow keys will cause a transfer of
 * focus to the this Pane's parent.
 * 2. The ChildPane will resize its width to minimize space taken.
 * 
 * 
 * @author Joshua Lockerman
 * 
 */
public class ChildPane extends RootPane {
    
    private static final int ENDSPACE = 5;
    private final Component parent;
    private FontMetrics metrics;
    
    /**
     * 
     */
    public ChildPane(Component parent)
    {
        super();
        this.parent = parent;
        initialize();
    }
    
    /**
     * @param doc
     */
    public ChildPane(Component parent, StyledDocument doc)
    {
        super(doc);
        this.parent = parent;
        setOpaque(false);
        initialize();
    }
    
    private void initialize()
    {
        // The font metrics are used in the resizing code
        metrics = getFontMetrics(getFont());
        
        // Adds the listeners which change focus to a parent upon reaching the end of the ChildPan
        addKeyListener(new FocusUpKeyListener(this, parent));
        getDocument().addDocumentListener(new DocumentListener() {
            
            @Override
            public void insertUpdate(DocumentEvent e)
            {
                updateSize();
            }
            
            @Override
            public void removeUpdate(DocumentEvent e)
            {
                updateSize();
            }
            
            @Override
            public void changedUpdate(DocumentEvent e)
            {
                updateSize();
            }
            
        });
    }
    
    protected void updateSize()
    {
        try {
            String text = getDocument().getText(0, getDocument().getLength());
            setSize(metrics.stringWidth(text), getHeight());
            Dimension d = new Dimension(metrics.stringWidth(text) + ENDSPACE,
                    getHeight());
            setPreferredSize(d);
            setMaximumSize(d);
            revalidate();
            repaint();
            revalidate();
            repaintParent();
        }
        catch(BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @Override
    public void setFont(Font font)
    {
        super.setFont(font);
        metrics = getFontMetrics(getFont());
    }
    
    private void repaintParent()
    {
        parent.repaint();
        if(parent instanceof ChildPane) {
            ((ChildPane) parent).repaintParent();
        }
    }
    
}
